//
//  BaseComponent.swift
//  demo3
//
//  Created by hanweiguang on 2018/12/29.
//  Copyright © 2018年 hanweiguang. All rights reserved.
//

import Foundation
import WeexSDK

import Charts
class DemoComponent: WXComponent ,ChartViewDelegate{
     
    var chartView: PieChartView!
    var dataPoints = ["1-9", "10-19", "20-29", "30-39","40-49"]
    var values = [30.0, 100.0, 108.0, 60.0, 50.0]

    override func loadView() -> UIView {
        chartView = PieChartView()
        chartView.backgroundColor = UIColor.yellow
        
        var dataEntries: [ChartDataEntry] = []
        for i in 0..<dataPoints.count {
            let entry = PieChartDataEntry(value: values[i], label: "\(dataPoints[i])") //设置数据 title和对应的值
            
            dataEntries.append(entry)
        }
        
        let pichartDataSet = PieChartDataSet(values: dataEntries, label: "") //设置表示
//        setPieChartDataSetConfig(pichartDataSet: pichartDataSet)
        let pieChartData = PieChartData(dataSet: pichartDataSet)
        //设置饼状图字体样式
        setPieChartDataConfig(pieChartData: pieChartData)
        chartView.data = pieChartData
        pichartDataSet.colors = [UIColor.yellow,UIColor.cyan,UIColor.red ]
  
        return chartView
    }
    
//    func setPieChartDataSetConfig(pichartDataSet: PieChartDataSet){
//        pichartDataSet.sliceSpace = 0 //相邻区块之间的间距
//        pichartDataSet.selectionShift = 1 //选中区块时, 放大的半径
//        pichartDataSet.xValuePosition = .insideSlice //名称位置
//        pichartDataSet.yValuePosition = .outsideSlice //数据位置
//        //数据与区块之间的用于指示的折线样式
//        pichartDataSet.valueLinePart1OffsetPercentage = 0.85 //折线中第一段起始位置相对于区块的偏移量, 数值越大, 折线距离区块越远
//        pichartDataSet.valueLinePart1Length = 0.5 //折线中第一段长度占比
//        pichartDataSet.valueLinePart2Length = 0.4 //折线中第二段长度最大占比
//        pichartDataSet.valueLineWidth = 1 //折线的粗细
//        pichartDataSet.valueLineColor = UIColor.lightGray //折线颜色
//    }
    
    //设置饼状图字体样式
    func setPieChartDataConfig(pieChartData: PieChartData){
        pieChartData.setValueFormatter(DigitValueFormatter())//设置百分比
        pieChartData.setValueTextColor(UIColor.gray) //字体颜色为白色
        pieChartData.setValueFont(UIFont.systemFont(ofSize: 10))//字体大小
    }
    
    public class DigitValueFormatter: NSObject, IValueFormatter {
        public func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
            let valueWithoutDecimalPart = String(format: "%.2f%%", value)
            return valueWithoutDecimalPart
            
        }
    }
   
    override func viewDidLoad() {
        chartView.delegate = self
  
    }
 
    override func addEvent(_ eventName: String) {
        // 添加事件
    }
    
    override func removeEvent(_ eventName: String) {
        // 移除事件
    }
    
    override init(ref: String, type: String, styles: [AnyHashable : Any]?, attributes: [AnyHashable : Any]? = nil, events: [Any]?, weexInstance: WXSDKInstance) {
        super.init(ref: ref, type: type, styles: styles, attributes: attributes, events: events, weexInstance: weexInstance)
        if let showsTraffic = attributes?["showsTraffic"] as? Bool {
            // 拿到了自定义属性
        }
    }
    
}

